/**
 * @author jprieur
 */
package mygui;

import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import utils.*;
import kernel.*;

public class ActionSave extends AbstractAction{

    MonIHM _gui;

    public ActionSave (MonIHM gui){
	_gui = gui;
	putValue(AbstractAction.NAME,"Save");
	putValue(AbstractAction.MNEMONIC_KEY,new Integer(KeyEvent.VK_S));
	putValue(AbstractAction.SHORT_DESCRIPTION,"Save the current simulation");
	putValue(AbstractAction.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_MASK ));
	putValue(AbstractAction.SMALL_ICON,UIM.createImageIcon("images/save.png","Add Mobile Station"));
    }
 
    public void actionPerformed(ActionEvent e) {
	JFileChooser fc = new JFileChooser();
 	fc.setFileFilter(new SimmobilFileFilter("sim" + Simulator.getSimulator().getMode()));

	if(fc.showSaveDialog(_gui) == JFileChooser.APPROVE_OPTION){
	    String path = fc.getSelectedFile().getAbsolutePath() + "." + "sim" + Simulator.getSimulator().getMode();
	    try {
		BufferedWriter bw = new BufferedWriter(new FileWriter(path));
		bw.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n<!DOCTYPE simmobil SYSTEM \"simmobil.dtd\">\n");
		bw.write(Simulator.getSimulator().xmlDump());
		bw.write("\n</simmobil>");
		bw.close();
	    }catch(IOException x){
		System.out.println(x);
	    }
	}
    }

}
